package tools

import (
	"fmt"
	"strconv"
	"strings"
	"time"
)

func GetUnixTime() string {
	// 当前时间戳
	now := time.Now().Unix()
	return strconv.FormatInt(now, 10)
}

// 获取天数加n后的日期
func GetNextDay(year, month, day string, n int) (string, string, string) {
	//获取当前时间
	t := time.Date(toInt(year), time.Month(toInt(month)), toInt(day), 0, 0, 0, 0, time.Local)
	//获取当前时间戳
	timestamp := t.Unix()
	//获取一天的时间戳
	oneday := int64(60 * 60 * 24 * n)
	//获取加一天后的时间戳
	nextday := timestamp + oneday
	//转换为时间格式
	tm := time.Unix(nextday, 0)
	//获取年月日
	year = strconv.Itoa(tm.Year())
	month = strconv.Itoa(int(tm.Month()))
	day = strconv.Itoa(tm.Day())
	if len(month) == 1 {
		month = "0" + month
	}
	if len(day) == 1 {
		day = "0" + day
	}
	return year, month, day
}

func toInt(year string) int {
	i, _ := strconv.Atoi(year)
	return i
}

// 获取下一秒的时间
func GetNextSecond(year, month, day, hour, minute, second string) string {
	//获取当前时间
	t := time.Date(toInt(year), time.Month(toInt(month)), toInt(day), toInt(hour), toInt(minute), toInt(second), 0, time.Local)
	//获取当前时间戳
	timestamp := t.Unix()
	//获取一秒的时间戳
	oneSecond := int64(1)
	//获取加一秒后的时间戳
	nextSecond := timestamp + oneSecond
	//转换为时间格式
	tm := time.Unix(nextSecond, 0)
	//获取年月日
	year = strconv.Itoa(tm.Year())
	month = strconv.Itoa(int(tm.Month()))
	day = strconv.Itoa(tm.Day())
	hour = strconv.Itoa(tm.Hour())
	minute = strconv.Itoa(tm.Minute())
	second = strconv.Itoa(tm.Second())

	//	<10的数字前面加0

	if len(month) == 1 {
		month = "0" + month
	}
	if len(day) == 1 {
		day = "0" + day
	}
	if len(hour) == 1 {
		hour = "0" + hour
	}
	if len(minute) == 1 {
		minute = "0" + minute
	}
	if len(second) == 1 {
		second = "0" + second
	}

	return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
}

//func GetNextDay(year, month, day string) (string, string, string) {
//	//获取当前时间
//	t := time.Date(toInt(year), time.Month(toInt(month)), toInt(day), 0, 0, 0, 0, time.Local)
//	//获取当前时间戳
//	timestamp := t.Unix()
//	//获取一天的时间戳
//	oneday := int64(60 * 60 * 24)
//	//获取加一天后的时间戳
//	nextday := timestamp + oneday
//	//转换为时间格式
//	tm := time.Unix(nextday, 0)
//	//获取年月日
//	year = strconv.Itoa(tm.Year())
//	month = strconv.Itoa(int(tm.Month()))
//	day = strconv.Itoa(tm.Day())
//	return year, month, day
//}
//
//func toInt(year string) int {
//	i, _ := strconv.Atoi(year)
//	return i
//}

func ExcelDateToDate(excelDate float64) string {
	//baseDate := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
	//days := int(excelDate)
	//seconds := int((excelDate - float64(days)) * 86400)
	//date := baseDate.Add(time.Duration(days-2) * time.Hour * 24).Add(time.Duration(seconds) * time.Second)
	//dateStr := date.Format("2006-01-02 15:04:05.0")

	//格式化到毫秒
	excelTime := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
	goTime := excelTime.Add(time.Duration(float64(time.Hour) * 24 * excelDate))
	// 格式化时间为字符串
	//formattedTime := goTime.Format("2006-01-02 15:04:05.000")
	//时间向前偏移两天
	goTime = goTime.Add(time.Duration(float64(time.Hour) * 24 * -2))
	formattedTime := goTime.Format("2006-01-02 15:04:05.000")
	//return formattedTime
	//是哦也能够.分割字符串
	timeArr := strings.Split(formattedTime, ".")
	mainT := timeArr[0]
	msT := timeArr[1]
	//msT转为int
	ms, _ := strconv.Atoi(msT)
	if ms == 499 {
		msT = "500"
	} else if ms == 999 {
		msT = "000"
		//	最后一位加1
		//mainTArr := strings.Split(mainT, ":")
		//lastNum, _ := strconv.Atoi(mainTArr[2])
		//lastNum += 1
		//if lastNum < 10 {
		//	mainTArr[2] = "0" + strconv.Itoa(lastNum)
		//} else {
		//	mainTArr[2] = strconv.Itoa(lastNum)
		//}
		//mainT = strings.Join(mainTArr, ":")

		mainT = GetNextSecond(mainT[0:4], mainT[5:7], mainT[8:10], mainT[11:13], mainT[14:16], mainT[17:19])
	}
	return mainT + "." + msT
	//return dateStr
}

// 整数取绝对值
func AbsInt(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

// 判断一个时间是否为整半个小时，并且分钟、秒数和毫秒数都为0
func IsHalfHour(t string) bool {
	tArr := strings.Split(t, ":")
	minute, _ := strconv.Atoi(tArr[1])
	if minute != 30 && minute != 0 {
		return false
	}
	if tArr[2] != "00.000" {
		return false
	}
	return true
}

// 格式化时间7/27/23 23:30 转为2023-07-27 23:30
func FormatTime(timeStr string) string {

	// 定义要解析的时间字符串
	//timeStr := "7/22/23 20:10"

	// 定义要解析的时间字符串的格式
	timeLayout := "1/2/06 15:04"

	// 将时间字符串解析为时间对象
	t, err := time.Parse(timeLayout, timeStr)
	if err != nil {
		fmt.Println("解析时间字符串失败：", err)
		return ""
	}

	// 定义要格式化的时间字符串的格式
	formatLayout := "2006-01-02 15:04"

	// 将时间对象格式化为字符串
	formattedTime := t.Format(formatLayout)

	return formattedTime

}

// 字符串时间转为时间戳
func StrToUnixTime(timeStr string) string {
	// 定义要解析的时间字符串的格式
	timeLayout := "2006-01-02 15:04:05"
	// 将时间字符串解析为时间对象
	t, err := time.ParseInLocation(timeLayout, timeStr, time.Local)
	if err != nil {
		fmt.Println("解析时间字符串失败：", err)
		return ""
	}
	return strconv.FormatInt(t.Unix(), 10)
}
